package com.sskj.aikeeper.login;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.sskj.aikeeper.R;
import com.sskj.common.base.App;
import com.sskj.common.rxbus2.RxBus;
import com.sskj.common.util.CheckUtil;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.util.EditUtil;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subscribers.DisposableSubscriber;

/**
 * @author Hey
 * Create at  2019/06/25
 */
@Route(path = RConfig.APP_FORGET_NUMBER)
public class GorgetPosswordActivity extends BaseActivity<GorgetPosswordPresenter> {

    @BindView(R.id.forget_number_et)
    EditText forgetNumberEt;
    @BindView(R.id.forget_code_et)
    EditText forgetCodeEt;
    @BindView(R.id.forget_getCode)
    TextView forgetGetCode;
    @BindView(R.id.forget_password_et)
    EditText forgetPasswordEt;
    @BindView(R.id.forget_next_password_et)
    EditText forgetNextPasswordEt;
    @BindView(R.id.forget_yincang_next_iv)
    ImageView forgetYincangNextIv;
    @BindView(R.id.forget_bt)
    Button forgetBt;
    @BindView(R.id.forget_return_iv)
    ImageView forgetReturnIv;
    @BindView(R.id.iv_password)
    ImageView ivPassword;

    @Override
    public int getLayoutId() {
        return R.layout.app_activity_gorget_possword;
    }

    @Override
    public GorgetPosswordPresenter getPresenter() {
        return new GorgetPosswordPresenter();
    }

    @Override
    public void initView() {
        RxBus.getDefault().register(this);
        //修改密码按钮
        ClickUtil.click(300, forgetBt, () -> {
            if (isEmpty(forgetNumberEt)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.app_app_activity_register190));
                return;
            }
            if (isEmpty(forgetPasswordEt)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.app_app_activity_change_login180));
                return;
            }
            if (isEmpty(forgetNextPasswordEt)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.app_app_activity_gorget_possword320));
                return;
            }
            if (isEmpty(forgetCodeEt)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.app_app_activity_change_pay120));
                return;
            }
            if (!CheckUtil.isMobile(forgetNumberEt.getText().toString())) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.app_loginActivity1));
                return;
            }
            if (!CheckUtil.isLoginPwd(forgetPasswordEt.getText().toString())) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.app_loginActivity2));
                return;
            }
            if (!CheckUtil.isLoginPwd(forgetNextPasswordEt.getText().toString())) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.app_loginActivity2));
                return;
            }
            mPresenter.forgetPassword(
                    forgetNumberEt.getText().toString(),
                    forgetPasswordEt.getText().toString(),
                    forgetNextPasswordEt.getText().toString(),
                    forgetCodeEt.getText().toString()
            );
        });
        ClickUtil.click(300, ivPassword, () -> {
            EditUtil.togglePs(forgetPasswordEt, ivPassword);
        });

        ClickUtil.click(300, forgetYincangNextIv, () -> {
            EditUtil.togglePs(forgetNextPasswordEt, forgetYincangNextIv);
        });

        ClickUtil.click(300, forgetGetCode, () -> {
            if (isEmpty(forgetNumberEt)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.app_app_activity_register190));
                return;
            }
            mPresenter.getForgetCode(forgetNumberEt.getText().toString());
        });

        ClickUtil.click(300, forgetReturnIv, this::finish);
    }

    @Override
    public void initData() {

    }

    public static void start(Context context) {
        Intent intent = new Intent(context, GorgetPosswordActivity.class);
        context.startActivity(intent);
    }

    public void forgetPasswordSuccess() {
        finish();
    }

    public void forgetCode() {
        startCount();
    }

    public void startCount() {
        forgetGetCode.setEnabled(false);
        disposableSubscriber = new DisposableSubscriber<Long>() {
            @Override
            public void onNext(Long aLong) {
                int time = (int) (60 - aLong);
                forgetGetCode.setText(time + App.INSTANCE.getString(R.string.libGet_code_wait));
            }

            @Override
            public void onError(Throwable t) {
                System.out.println(t.toString());
            }

            @Override
            public void onComplete() {
                forgetGetCode.setText(App.INSTANCE.getString(R.string.libIdCardVerification115));
                forgetGetCode.setEnabled(true);
                if (!disposableSubscriber.isDisposed()) {
                    disposableSubscriber.dispose();
                    disposableSubscriber = null;
                }
            }
        };

        Flowable.interval(0, 1, TimeUnit.SECONDS, Schedulers.newThread())
                .take(60)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(disposableSubscriber);
    }
}
